<?php

namespace App\Http\Controllers\Api;

use App\Models\Collection;
use App\Models\Goods;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class GoodsController extends Controller
{
    public function detail(Request $request)
    {

        $goods = Goods::find($request->get('goods_id'));
        $cover = $goods->my_picture()->where('type',1)->first();
        $banners = $goods->my_picture()->where('type',2)->get();
        foreach ($banners as $banner){
            $banner_arr[]=$banner->my_pic_path;
        };
        $goods->cover = $cover->my_pic_path;
        $goods->banner=$banner_arr;
        if($request->has('token')){
            $user = User::where('token',$request->get('token'))->first();
            $collection = Collection::where([['goods_id',$request->get('goods_id')],['user_id',$user->user_id]])->first();
            $collection?$goods->is_collection=1:$goods->is_collection=0;
        }
        return ['status_code'=>1,'data'=>$goods];
    }

    public function lists(Request $request)
    {
        $where = [
            ['status',1],
        ];
        if($request->has('classify_id')){
            array_push($where,['class_id',$request->get('classify_id')]);
        }
        $goods = Goods::where($where)->select(['name','price','goods_id','goods_sn'])->orderBy('goods_id','desc')->paginate(10);
        foreach ($goods as $value){
            $cover = $value->my_picture()->where('type',1)->first();
            $value->cover = $cover->my_pic_path;
        }
        return ['status_code'=>1,'data'=>$goods];
    }

    public function all()
    {
        $goods = Goods::select(['name','price','goods_id','goods_sn'])->orderBy('goods_id','desc')->paginate(10);
        foreach ($goods as $value){
            $cover = $value->my_picture()->where('type',1)->first();
            $value->cover = $cover->my_pic_path;
        }
        return ['status_code'=>1,'data'=>$goods];
    }

}
